ComposableView: custom Android views and Compose functions - #39
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The library's
UIViewRepresentable-equivalent. Because a@Composable(or an AndroidView) can never be authored in Swift, the seam is a named registry: Kotlin registersname → composablefactories once at startup, and Swift references one by name.API
Swift —
ComposableView(_ name:props:content:)emits a node that references a registered factory, forwarding typed props and an optional slot of SwiftUI child content:PropValuegained literal conveniences, so props read as a plain dictionary (["url": "…", "zoom": 1.5, "on": true]).Kotlin —
ComposableRegistry.register(name) { props, children -> … }. A factory that wraps a legacy AndroidView(maps, WebView, an ad SDK) uses Compose's ownAndroidViewinside;propsis a typed accessor (props.float("rating")), so app authors never touch the JSON layer. Unregistered names render a visible diagnostic.The interesting bug
Child content initially rendered nothing. Root cause (found by bisecting the render path on-device): a nested call to a public composable from a dynamically-invoked factory slot has its restart group skipped by the Compose runtime — the subtree silently fails to compose. Fixed by routing the registry slot through an
internalRenderChildentry point; the publicRendernow delegates to it. (Also: the registry now stores a plain composable function type rather than afun interface.)Verification
swift test— 2 new emission tests (typed props + child forwarding), 60 total passingandroid.widget.RatingBarthat reactively updates as the Swift@Statechanges (3.5 → 4.5 via ±½ buttons); a ComposeDashedBorderdrawing around two SwiftUI Texts in its slot; and the red unregistered-name diagnosticDeferred
Two-way callbacks from a custom composable back into Swift state (props flow Swift→Kotlin only for now); desktop registration of Android-view factories (they're Android-only by nature).